new Vue({
    el: '#app',
    data() {
        return {
            form: {
                Page_Services_SerialNumber: '',
                Page_Services_Title: '',
                Page_Services_Type: '',
                Page_Services_Route: '',
                Page_Services_Icon: '',
                Page_Services_Notes: '',
            },
            rules: {
                Function_Approval_Name: [{ required: true, message: '请输入功能名称', trigger: 'blur' }],
                Function_Approval_Code: [{ required: true, message: '请输入功能Code', trigger: 'blur' }],
            },
            page_services_type: [{
                label: '业务服务',
                value: '0',
            }, {
                label: '金融服务',
                value: '1',
            }],
            loading: false,
        }
    },
    mounted() {
        const Page_Services_SerialNumber = sessionStorage.getItem("Page_Services_SerialNumber")
        this.getData(Page_Services_SerialNumber)
    },
    methods: {
        getData(Primary_Key) {
            const _this = this;
            $.ajax({
                url: DomainName + '/getInquiryIDOriginal?token=' + $.cookie('token'),
                type: 'POST',
                data: {
                    Data: 'page_services_info',
                    Primary_Key,
                    Primary_Key_Name: 'Page_Services_SerialNumber'
                },
                success: function (res) {
                    if (res.length > 0) {
                        _this.form = res[0];
                    }
                },
                error: function () {
                    _this.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                    _this.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
        },
        submitForm(formName) {
            let _this = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    _this.loading = true
                    const { add, modify } = _this.deal()
                    let data = {}
                    if (add == null) {
                        data = { Data: JSON.stringify({ Modify: { page_services_info: [modify] } }) }
                    } else {
                        data = {
                            Data: JSON.stringify({ Modify: { page_services_info: [modify] }, Add: { page_services_info: [add] } })
                        }
                    }
                    $.ajax({
                        url: DomainName + '/getEditAction?token=' + $.cookie('token'),
                        type: 'POST',
                        data: { ...data, Operation_Name: '修改服务' },
                        success: function (res) {
                            if (res.Status === 'success') {
                                _this.$message.success("提交成功")
                                window.history.go(-1)
                            } else {
                                _this.$message.error("提交失败，请稍后再试")
                            }
                        },
                        error: function () {
                            _this.$message.error("操作失败，请稍后再试")
                        },
                        complete: function (XMLHttpRequest) {
                            _this.loading = false
                            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                                window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                            }
                        }
                    })
                } else {
                    return false;
                }
            });
        },
        deal() {
            const modify = deepClone(this.form)
            return { add: null, modify };
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    }
})